layui.define(['jquery','layer','form','element'],function(exports){
    var $=layui.jquery;
    var layer=layui.layer;
    var form =layui.form();
    var element=layui.element();
    var check_menus=function(){};

    form.on('checkbox(check_all)', function(data){
        if(data.elem.checked){
            $("input[name='mu']").prop("checked", true);
        }else{
            $("input[name='mu']").prop("checked", false);
        }  
        form.render('checkbox');      
    }); 
    form.on('checkbox(check_cld)', function(data){
        if(data.elem.checked){
            $("input[data-id='" + data.value + "']").each(function(i, obj) {
				$(obj).prop('checked', true);
			});
        }else{
            $("input[data-id='" + data.value + "']").each(function(i, obj) {
				$(obj).prop('checked', false);
			});
        }  
        form.render('checkbox');      
    }); 
    $.post("/srv/rdo.php?", {tpl:"get_ck_menu"}, function (d, e) {
        $("#div_role").empty();
        $("#div_role").append(d);
    }); 
    form.on('checkbox(check_th)', function(data){
        var pid=$(data.elem).attr('data-id');
        if(data.elem.checked){
            $("input[value='"+pid+"']").prop("checked", true);
        }else{
            var blg=false;
            $("input[data-id='" + pid + "']").each(function(i, obj) {
				if($(obj).prop('checked')){
                    blg=true;
                }
			});
            if(!blg){
                $("input[value='"+pid+"']").prop("checked", false);
            }
        }  
        form.render('checkbox');      
    }); 
    $("#btnSave").click(function(){
        var m_val="";
        var index = parent.layer.getFrameIndex(window.name);
        $("[name='mu']:checked").each(function(i, obj) {
			m_val+=$(this).val()+",";
		});
        if(m_val.lastIndexOf(",")>0){
            m_val=m_val.substring(0,m_val.length-1) ;
        }
        var id=$("#id").val();
        if(m_val.length>0){ 
            $.post("/srv/sdo.php", {tpl:"add_role_menu",id:id,mid:m_val}, function (d, e) {  
                    if(d=="ok"){
                        layer.msg("保存成功！");
                        setTimeout("parent.layer.close("+index+")",1000);
                    }
            });
        }else{
            layer.msg('没有选择数据');
        } 
    });

    check_menus.prototype.init=function(){
        var mid=$("#muid").val();
        if(mid!=null && mid!=""){
            var arrList= new Array();   
            arrList=mid.split(",");    
            $("input[name='mu']").each(function(i, obj) {
                var vl=$(this).val(); 
                for (i=0;i<arrList.length ;i++ ){
                    if(arrList[i]==vl){
                        $(obj).prop('checked', true);
                    }
                } 
            });
            form.render('checkbox');    
        }
    }

    var checkMenus=new check_menus();
    checkMenus.init();

    exports('system_check_menu',checkMenus);
});